﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PhotoList.Dal;
using QT.Basic;

namespace PhotoList.Web.Admin
{
    public partial class ucUser : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetUserList();
            }
        }

        #region User

        private void GetUserList()
        {
            DataTable dt = PhotoList.Dal.DBO.LoadUserList();

            this.gvList.DataSource = dt;
            this.gvList.DataBind();
        }

        private void AddUser()
        {
            PhotoList.Dal.User uUser = new PhotoList.Dal.User();
            uUser.Uid = this.tbUserid.Text.Trim();
            uUser.Pass = new QT.Basic.Tools().MD5(this.tbPass.Text.Trim());
            uUser.Home = this.tbHome.Text.Trim();
            uUser.Name = this.tbName.Text.Trim();
            uUser.Group = this.ddlGourp.SelectedValue.Trim();
            if(uUser.AddUser())
            {
                this.divAdd.Visible = false;
                new QT.Basic.Tools().Alert("添加成功");
                GetUserList();
            }
            else
            {
                new QT.Basic.Tools().Alert("操作失败");
            }
        }

        private void ModiUser(string uid)
        {
            PhotoList.Dal.User uUser = new PhotoList.Dal.User();
            uUser.Uid = this.tbUserid.Text.Trim();
            uUser.Pass = new QT.Basic.Tools().MD5(this.tbPass.Text.Trim());
            uUser.Home = this.tbHome.Text.Trim();
            uUser.Name = this.tbName.Text.Trim();
            uUser.Group = this.ddlGourp.SelectedValue.Trim();
            if(uUser.ModiUser())
            {
                this.divAdd.Visible = false;
                new QT.Basic.Tools().Alert("修改成功");
                GetUserList();
            }
            else
            {
                new QT.Basic.Tools().Alert("操作失败");
            }
        }

        private void DeleUser(string uid)
        {
            PhotoList.Dal.User uUser = new PhotoList.Dal.User();
            uUser.Uid = uid;
            if (uUser.DeleUser())
            {
                new QT.Basic.Tools().Alert("删除成功");
                GetUserList();
            }
            else
            {
                new QT.Basic.Tools().Alert("操作失败");
            }
        }

        #endregion

        #region Button

        protected void btnOpen_Click(object sender, EventArgs e)
        {
            this.lblTitle.Text = "添加新用户";
            this.btn_OK.Visible = true;
            this.btn_Modi.Visible = false;
            this.divAdd.Visible = true;
        }

        protected void btn_OK_Click(object sender, EventArgs e)
        {

            Regex rgx = new Regex(@"^[a-zA-Z][a-zA-Z0-9_.]{1,8}[a-zA-Z0-9]$");
            if (!rgx.IsMatch(this.tbUserid.Text))
            {
                new Tools().Alert("输入的用户名不合法！规则如下：\\n\\n只能包含字母、数字和下划线\\n必须以字母开始\\n长度为3至10个字符");
                return;
            }
            if (this.tbHome.Text.Length == 0)
            {
                new Tools().Alert("请设置主目录");
                return;
            }
            AddUser();
        }

        protected void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.divAdd.Visible = false;
        }

        protected void lbModi_Click(object sender, EventArgs e)
        {
            string uid = ((LinkButton)sender).CommandArgument;

            PhotoList.Dal.User uUser = new PhotoList.Dal.User(uid);
            this.lblTitle.Text = "用户信息修改";
            this.tbUserid.Text = uUser.Uid;
            this.tbUserid.ReadOnly = true;
            this.tbPass.Text = "";
            this.tbHome.Text = uUser.Home;
            this.tbName.Text = uUser.Name;
            this.ddlGourp.SelectedValue = uUser.Group;

            this.btn_OK.Visible = false;
            this.btn_Modi.Visible = true;
            this.divAdd.Visible = true;
        }

        protected void btn_Modi_Click(object sender, EventArgs e)
        {
            ModiUser(this.tbUserid.Text.Trim());
        }

        protected void lbDele_Click(object sender, EventArgs e)
        {
            string uid = ((LinkButton)sender).CommandArgument;
            DeleUser(uid);
        }

        protected void lbPass_Click(object sender, EventArgs e)
        {
            string uid = ((LinkButton)sender).CommandArgument;
            Response.Redirect("Main.aspx?target=User/ucChangePwd&uid=" + uid);
        }

        #endregion
    }
}